//
//  ChartContentView.m
//  气泡
//
//  Created by zzy on 14-5-13.
//  Copyright (c) 2014年 zzy. All rights reserved.
//
#define kContentStartMargin 25
#import "ChartContentView.h"
#import "ChartMessage.h"

@implementation ChartContentView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //self.backgroundColor = [UIColor redColor];
        self.backImageView=[[UIImageView alloc] init];
        self.backImageView.userInteractionEnabled = YES;
        [self addSubview:self.backImageView];
        
        self.contentLabel = [[UILabel alloc]init];
        self.contentLabel.numberOfLines = 0;
        self.contentLabel.lineBreakMode = NSLineBreakByCharWrapping;
        self.contentLabel.font = [UIFont systemFontOfSize:15];
        self.contentLabel.textAlignment = NSTextAlignmentLeft;
        self.contentLabel.backgroundColor = [UIColor clearColor];
        [self addSubview:self.contentLabel];
        
        self.contentTextView = [[CFTextView alloc]init];
        self.contentTextView.editable = NO;
        self.contentTextView.textAlignment = NSTextAlignmentLeft;
        self.contentTextView.font = [UIFont systemFontOfSize:15];
        self.contentTextView.backgroundColor = [UIColor clearColor];
        self.contentTextView.scrollEnabled = NO;
        [self addSubview:self.contentTextView];
        
        //发送图片使用
        self.messageImageView = [[UIImageView alloc] init];
        self.messageImageView.backgroundColor = [UIColor clearColor];
        [self.backImageView addSubview:self.messageImageView];
        
        
        self.fileIconView = [[UIImageView alloc]init];
        self.fileIconView.backgroundColor = [UIColor clearColor];
        [self addSubview:self.fileIconView];
        
        self.fileNameLabel = [[UILabel alloc]init];
        self.fileNameLabel.font = [UIFont systemFontOfSize:14];
        self.fileNameLabel.backgroundColor = [UIColor clearColor];
        self.fileNameLabel.textColor = [UIColor darkGrayColor];
        [self addSubview:self.fileNameLabel];
        
        self.fileSizeLabel = [[UILabel alloc]init];
        self.fileSizeLabel.font = [UIFont systemFontOfSize:12];
        self.fileSizeLabel.backgroundColor = [UIColor clearColor];
        self.fileSizeLabel.textColor = [UIColor darkGrayColor];
        [self addSubview:self.fileSizeLabel];
        
    }
    return self;
}

-(void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
    self.backImageView.frame = self.bounds;
    self.contentLabel.textColor = [UIColor blackColor];
    self.contentTextView.textColor = [UIColor blackColor];
    
    if (self.chartMessage.messageStyle == kMessageText) {
        self.contentLabel.textAlignment = NSTextAlignmentLeft;
        self.contentTextView.textAlignment = NSTextAlignmentLeft;
        CGFloat contentLabelX = 0;
        if(self.chartMessage.messageType == kMessageFrom)
        {
            contentLabelX = kContentStartMargin*0.8;
            self.contentLabel.frame = CGRectMake(17, 12, self.frame.size.width-28, self.backImageView.frame.size.height - 24);
            self.contentTextView.frame = CGRectMake(10, 6, self.frame.size.width-20, self.backImageView.frame.size.height - 12);
        }
        else if(self.chartMessage.messageType==kMessageTo)
        {
            contentLabelX = kContentStartMargin*0.5;
            self.contentLabel.frame = CGRectMake(12, 12, self.frame.size.width-28, self.backImageView.frame.size.height - 24);
            self.contentTextView.frame = CGRectMake(10, 6, self.frame.size.width-20, self.backImageView.frame.size.height - 12);
        }
        [self showOrHideFileView:YES];

    }else if (self.chartMessage.messageStyle == kMessageImage){
        self.contentLabel.textAlignment = NSTextAlignmentCenter;
        self.contentLabel.textColor = [UIColor whiteColor];
        self.contentLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
        self.contentTextView.textAlignment = NSTextAlignmentCenter;
        self.contentTextView.textColor = [UIColor whiteColor];
        self.contentTextView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
        if(self.chartMessage.messageType == kMessageFrom)
        {
            self.messageImageView.frame = CGRectMake(7, 2, self.bounds.size.width - 9, self.bounds.size.height - 4);
        }
        else if(self.chartMessage.messageType == kMessageTo)
        {
            self.messageImageView.frame = CGRectMake(2, 2, self.bounds.size.width - 9, self.bounds.size.height - 4);
        }
        [self showOrHideFileView:YES];

    }else if (self.chartMessage.messageStyle == kMessageAudio){
        if(self.chartMessage.messageType == kMessageFrom)
        {
            self.messageImageView.frame = CGRectMake(17, (CGRectGetHeight(self.frame) - 20)/2, 13, 20);
            self.contentLabel.frame = CGRectMake(self.frame.size.width - 12 - 30, 0, 30, CGRectGetHeight(self.frame));
            self.contentLabel.textAlignment = NSTextAlignmentRight;
            
            self.contentTextView.frame = CGRectMake(self.frame.size.width - 12 - 30, 0, 30, CGRectGetHeight(self.frame));
            self.contentTextView.textAlignment = NSTextAlignmentRight;
        }
        else if(self.chartMessage.messageType == kMessageTo)
        {
            self.messageImageView.frame = CGRectMake(self.frame.size.width - 12 - 13, (CGRectGetHeight(self.frame) - 20)/2, 13, 20);
            self.contentLabel.frame = CGRectMake(12, 0, 30, CGRectGetHeight(self.frame));
            self.contentLabel.textAlignment = NSTextAlignmentLeft;
            
            self.contentTextView.frame = CGRectMake(12, 0, 30, CGRectGetHeight(self.frame));
            self.contentTextView.textAlignment = NSTextAlignmentLeft;
        }
        [self showOrHideFileView:YES];

    }else if (self.chartMessage.messageStyle == kMessageVideo){
        
        self.contentLabel.textAlignment = NSTextAlignmentCenter;
        self.contentLabel.textColor = [UIColor whiteColor];
        self.contentLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
        
        self.contentTextView.textAlignment = NSTextAlignmentCenter;
        self.contentTextView.textColor = [UIColor whiteColor];
        self.contentTextView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
        if(self.chartMessage.messageType == kMessageFrom){
            self.messageImageView.frame = CGRectMake(7, 2, self.bounds.size.width - 9, self.bounds.size.height - 4);
        }else if(self.chartMessage.messageType == kMessageTo){
            self.messageImageView.frame = CGRectMake(2, 2, self.bounds.size.width - 9, self.bounds.size.height - 4);
        }
        [self showOrHideFileView:YES];

        
    }else if (self.chartMessage.messageStyle == kMessageCustom){
        self.messageImageView.hidden = YES;
        self.contentLabel.hidden = YES;
        self.contentTextView.hidden = YES;
        [self showOrHideFileView:NO];
        self.fileIconView.frame = CGRectMake(10, 14, 40, 40);
        self.fileNameLabel.frame = CGRectMake(CGRectGetMaxX(self.fileIconView.frame)+10, 5, frame.size.width - 40 - 10 *4, 30);
        self.fileSizeLabel.frame = CGRectMake(CGRectGetMaxX(self.fileIconView.frame)+10, CGRectGetMaxY(self.fileNameLabel.frame) + 5 , 100, 20);
        self.fileIconView.image = [UIImage imageNamed:@"sendFileBg"];
    }
}
/**
 *  是否隐藏文件相关的cell
 *
 *  @param hide YES 隐藏  NO 显示
 */
- (void)showOrHideFileView:(BOOL)hide
{
    self.fileIconView.hidden = hide;
    self.fileSizeLabel.hidden = hide;
    self.fileNameLabel.hidden = hide;
}



@end
